﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using FrameWorkSystem.BusinessLayer;
using CoreSystem;
namespace FrameWorkSystem
{
    public partial class frmUserNewEdit : BaseForm
    {
        bool isNew;
        string _UserGroupID;
        public EventHandler OnAddNewEntity, OnUpdateEntity;
        Sys_Users objEntity;

        public Sys_Users UserEntity
        {
            get { return objEntity; }
            set { objEntity = value; }
        }
        Sys_UsersFactory objDao;

        public string UserGroupID
        {
            get { return _UserGroupID; }
            set { _UserGroupID = value; }
        }
        public frmUserNewEdit()
        {
            InitializeComponent();

            isNew = true;
            objEntity = new Sys_Users();
            btnSave.Text = "Lưu và thêm mới";
            d_Tungay.Value = SysVariable.WorkingDate;
            d_Denngay.Value = SysVariable.WorkingDate.AddYears(2);
        }
        public frmUserNewEdit(Sys_Users _entity)
        {
            InitializeComponent();

            isNew = false;
            objEntity = _entity;
            btnSave.Text = "Lưu";

            txt_Uname.ReadOnly = true;            
            //SetBlankControl();
        }
        DataView dtGroup;
        private void frmUserNewEdit_Load(object sender, EventArgs e)
        {
            objDao = new Sys_UsersFactory();

            Sys_UserGroupsFactory objUGroupDao = new Sys_UserGroupsFactory();
            dtGroup = objUGroupDao.SelectAll().DefaultView;

            dtGroup.Sort = "ID_Group";
            cb_Nhom.SetDataSource(ref dtGroup, "Name", "ID_Group");
            
            if (!isNew)
            {
                _UserGroupID = objEntity.Group_ID;
                ViewEntity();
            }
            int id = dtGroup.Find(_UserGroupID);
            cb_Nhom.SelectedIndex = id;

            this.ActiveControl = txt_Uname;
        }
        private void ViewEntity()
        {
            txt_Uname.Text = objEntity.User_Name;
            txt_Password.Text = objEntity.Realpasswords;
            txt_hoten.Text = objEntity.Full_Name;
            //objEntity.Group_ID cb_Nhom.SelectedValue.ToString();
            d_Tungay.Value = objEntity.Start_Date;
            d_Denngay.Value = objEntity.End_Date;

            chk_Thu2.Checked = objEntity.Is_Mon;
            chk_Thu3.Checked = objEntity.Is_Tue;
            chk_Thu4.Checked = objEntity.Is_Wed;
            chk_Thu5.Checked = objEntity.Is_Thu;
            chk_Thu6.Checked = objEntity.Is_Fri;
            chk_Thu7.Checked = objEntity.Is_Sad;
            chk_Chunhat.Checked = objEntity.Is_Sun;

            chk_Sua.Checked = objEntity.IsNew;
            chk_Sua.Checked = objEntity.IsEdit;
            chk_Xoa.Checked = objEntity.IsDelete;
        }
        private void btnSave_Click(object sender, EventArgs e)
        {
            objEntity.User_Name= txt_Uname.Text.Trim();
            objEntity.Realpasswords = txt_Password.Text.Trim();
            objEntity.Full_Name = txt_hoten.Text;
            string group_id = cb_Nhom.SelectedValue == null ? "" : cb_Nhom.SelectedValue.ToString();
            objEntity.Group_ID = group_id;
            objEntity.Start_Date = d_Tungay.Value;
            objEntity.End_Date = d_Denngay.Value;

            objEntity.Is_Mon = chk_Thu2.Checked;
            objEntity.Is_Tue = chk_Thu3.Checked;
            objEntity.Is_Wed = chk_Thu4.Checked;
            objEntity.Is_Thu = chk_Thu5.Checked;
            objEntity.Is_Fri = chk_Thu6.Checked;
            objEntity.Is_Sad = chk_Thu7.Checked;
            objEntity.Is_Sun = chk_Chunhat.Checked;

            objEntity.IsNew = chk_Sua.Checked;
            objEntity.IsEdit = chk_Sua.Checked;
            objEntity.IsDelete = chk_Xoa.Checked;

            if (!objEntity.IsValid)
            {
                MessageBox.Show(objEntity.ErrorMsg);
                return;
            }           
            if (isNew)
            {
                if (objDao.CheckExistUser(objEntity.User_Name))
                {
                    MessageBox.Show("User đã tồn tại");
                    this.lblMsg.Text = "";
                    return;
                }
                if (objDao.Insert(objEntity))
                {
                    if (OnAddNewEntity != null) OnAddNewEntity(objEntity, new EventArgs());
                    SetBlankControl();
                    this.ActiveControl = txt_Uname;
                    this.lblMsg.Text = "Đã thêm User thành công";
                }
            }
            else
            {
                if (objDao.Update(objEntity))
                    if (OnUpdateEntity != null) OnUpdateEntity(objEntity, new EventArgs());
                this.Close();
            }
        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        private void SetBlankControl()
        {
            txt_Uname.Text = "";
            txt_Password.Text = "";
            txt_hoten.Text = "";
            cb_Nhom.SelectedIndex = -1;
            d_Tungay.Value = SysVariable.WorkingDate;
            d_Denngay.Value = SysVariable.WorkingDate.AddYears(2);

            chk_Thu2.Checked = true;
            chk_Thu3.Checked = true;
            chk_Thu4.Checked = true;
            chk_Thu5.Checked = true;
            chk_Thu6.Checked = true;
            chk_Thu7.Checked = true;
            chk_Chunhat.Checked = true;

            chk_Sua.Checked = true;
            chk_Sua.Checked = true;
            chk_Xoa.Checked = true;
        }
    }
}
